import { createRouter, createWebHashHistory } from 'vue-router';
import { usePermissStore } from '../store/permiss';
import Home from '@/layout/index.vue';
import NProgress from 'nprogress';
import 'nprogress/nprogress.css';

const routes: any = [
  {
    path: '/',
    redirect: '/dashboard'
  },
  {
    path: '/',
    name: 'Home',
    component: Home,
    children: [
      {
        path: '/dashboard',
        name: 'dashboard',
        meta: {
          title: '首页',
          noAuth: true
        },
        component: () => import('@/views/dashboard.vue'),
      },
      {
        path: '/scheduleTable',
        name: 'scheduleTable',
        meta: {
          title: '排班表',
          permiss: '20',
        },
        component: () => import('@/views/element-components/schedule.vue'),
      },
      {
        path: '/cascader',
        name: 'cascader',
        meta: {
          title: '级联组件',
          permiss: '21',
        },
        component: () => import('@/views/element-components/cascader.vue'),
      },
      {
        path: '/upload',
        name: 'upload',
        meta: {
          title: '上传',
          permiss: '22',
        },
        component: () => import('@/views/element-components/uploadPage.vue'),
      },
      {
        path: '/theme',
        name: 'theme',
        meta: {
          title: '主题设置',
          permiss: '7',
        },
        component: () => import(/* webpackChunkName: "theme" */ '@/views/pages/theme.vue'),
      },
    ]
  },
  {
    path: '/login',
    meta: {
      title: '登录',
      noAuth: true,
    },
    component: () => import(/* webpackChunkName: "login" */ '../views/pages/login.vue'),
  },
  {
    path: '/403',
    meta: {
      title: '没有权限',
      noAuth: true,
    },
    component: () => import(/* webpackChunkName: "403" */ '../views/pages/403.vue'),
  },
];

const router = createRouter({
    history: createWebHashHistory(),
    routes,
});

router.beforeEach((to, from, next) => {
  // NProgress.start();
  const role = localStorage.getItem('vuems_name');
  const permiss = usePermissStore();

  if (!role && to.meta.noAuth !== true) {
    next('/login');
  } else if (typeof to.meta.permiss == 'string' && !permiss.key.includes(to.meta.permiss)) {
    // 如果没有权限，则进入403
    next('/403');
  } else {
    next();
  }
});

// router.afterEach(() => {
//   NProgress.done();
// });



export default router;